/*
 * PagamentoTela2.java
 *
 * Created on 8 de Janeiro de 2008, 20:18
 */

package br.cefetrn.datinf.pdv.visao;
import br.cefetrn.datinf.estoque.dominio.Cartao;
import br.cefetrn.datinf.estoque.dominio.PagamentoCartao;
import br.cefetrn.datinf.estoque.dominio.PagamentoDinheiro;
import br.cefetrn.datinf.estoque.dominio.PagamentoCupomDeTroca;
import br.cefetrn.datinf.estoque.dominio.Parcela;
import br.cefetrn.datinf.estoque.dominio.TipoPagamento;
import br.cefetrn.datinf.estoque.dominio.Venda;
import br.cefetrn.datinf.pdv.ISistema;
import br.cefetrn.datinf.pdv.Sistema;
import cefetrn.datinf.tads.credito.exception.ValidacaoException;
import java.awt.event.KeyEvent;
import java.rmi.RemoteException;
import java.sql.SQLException;
import javax.swing.JOptionPane;


/**
 *
 * @author  Raquel
 */
public class PagamentoTela2 extends javax.swing.JFrame {
    private ISistema sistema = Sistema.getInstance();
  //  private Double entrada = 0.0;
    private Double restante = 0.0;
  //  private Double valorPago = 0.0;    
    private Venda venda = null;    
    
    private TipoPagamento tipoPagamento = null;
    
    
    
    /** Creates new form PagamentoTela2 */
    public PagamentoTela2(Venda venda) {
        initComponents();
        this.venda = venda;
        jText2.setText(venda.getValor()+"");
        this.restante = venda.getValor();
        
    }

    private void adicionarPagamanetoTroca() {
        
    }

    private void adicionarPagamentoCartao() {
        PagamentoCartao pagamentoCartao = new PagamentoCartao();
        pagamentoCartao.setTipo(TipoPagamento.Cartao);
      
           Double valorPgtoCartao = Double.parseDouble(jTextField4.getText());
           int nParcelas = Integer.parseInt(jTextField5.getText()) ;
           pagamentoCartao.setValor(valorPgtoCartao);
           pagamentoCartao.setNumeroParcelas(nParcelas);
           Cartao cartao = new Cartao();
           cartao.setId(Integer.parseInt(jTextField6.getText()));
           pagamentoCartao.setCartao(cartao);
           pagamentoCartao.setVenda(venda);
           venda.adicionarPagamento(pagamentoCartao);
           jTextField5.setText(nParcelas+" X "+valorPgtoCartao/nParcelas);
       //   try{
           sistema.solicitarAprovacaoDeCompra(jTextField6.getText(),Double.parseDouble(jTextField4.getText()) , Integer.parseInt(jTextField5.getText()), "Pdv");
           this.venda.adicionarPagamento(pagamentoCartao);
      //    } catch (ValidacaoException ex) {
      //      JOptionPane.showMessageDialog(null, "Crédito não autorizado");
      //    }catch (SQLException ex) {
      //      JOptionPane.showMessageDialog(null, "Erro de dado.Chame o administrador do sistema");
      //    }catch (RemoteException ex) {
            JOptionPane.showMessageDialog(null, "Credito aprovado");
         
        if(!jText3.getText().equals("")){
            JOptionPane.showMessageDialog(null, "vazio");
            PagamentoDinheiro pagamentoDinheiro = new PagamentoDinheiro();
            pagamentoDinheiro.setValor(Double.parseDouble(jText3.getText()));
            pagamentoDinheiro.setVenda(venda);
            venda.adicionarPagamento(pagamentoDinheiro);
            
        }
            
           
           
      // }
    }

    private void adicionarPagamentoDinheiro() {
        System.out.println("doooooooooooga");
        PagamentoDinheiro pagamentoDinheiro = new PagamentoDinheiro();
        Double valorPgto = Double.parseDouble(jTextField4.getText());
        pagamentoDinheiro.setTipo(TipoPagamento.Dinheiro);
        pagamentoDinheiro.setValor(venda.getValor());
        pagamentoDinheiro.setVenda(this.venda);
        venda.adicionarPagamento(pagamentoDinheiro);
        //para mostar
        sistema.finalizarVenda(venda);
       // jTextField5.setText(""+(valorPgto-venda.getValor()));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabelTipoPagamento = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jText1 = new javax.swing.JTextField();
        jText2 = new javax.swing.JTextField();
        jText3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jLabelTipo = new javax.swing.JLabel();
        jLabelTotal = new javax.swing.JLabel();
        EntradaOUValorCartOUNCupom = new javax.swing.JLabel();
        jLabelValorPagoOuParcelaOuValorCupom = new javax.swing.JLabel();
        jLabelCartaoOuTroco = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Pagamento");

        jLabelTipoPagamento.setFont(new java.awt.Font("Tahoma", 0, 36));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("F1 (À Vista)");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setText("F2 (Cartão)");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel5.setText("F3 (Troca)");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel6.setText("F4 (Confirmar)");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel7.setText("F5 (Encerrar)");

        jText1.setFont(new java.awt.Font("Tahoma", 0, 36));

        jText2.setFont(new java.awt.Font("Tahoma", 0, 36));

        jText3.setFont(new java.awt.Font("Tahoma", 0, 36));
        jText3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jText3KeyPressed(evt);
            }
        });

        jTextField4.setFont(new java.awt.Font("Tahoma", 0, 36));
        jTextField4.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField4KeyPressed(evt);
            }
        });

        jTextField5.setFont(new java.awt.Font("Tahoma", 0, 36));

        jLabelTipo.setFont(new java.awt.Font("Tahoma", 0, 36));
        jLabelTipo.setText("TIPO");

        jLabelTotal.setFont(new java.awt.Font("Tahoma", 0, 36));
        jLabelTotal.setText("TOTAL");

        EntradaOUValorCartOUNCupom.setFont(new java.awt.Font("Tahoma", 0, 36));
        EntradaOUValorCartOUNCupom.setText("Entrada");

        jLabelValorPagoOuParcelaOuValorCupom.setFont(new java.awt.Font("Tahoma", 0, 36));
        jLabelValorPagoOuParcelaOuValorCupom.setText("Valor PAGO");

        jLabelCartaoOuTroco.setFont(new java.awt.Font("Tahoma", 0, 36));
        jLabelCartaoOuTroco.setText("Troco");

        jTextField6.setFont(new java.awt.Font("Tahoma", 0, 36));

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 36));
        jLabel11.setText("Nº Cartão");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addContainerGap(179, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTipoPagamento)
                .addGap(248, 248, 248)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(EntradaOUValorCartOUNCupom)
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabelTotal)
                            .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jText3, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabelValorPagoOuParcelaOuValorCupom, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextField4, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabelCartaoOuTroco, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextField5, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextField6, javax.swing.GroupLayout.Alignment.LEADING))
                            .addGap(107, 107, 107))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jText2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                                .addComponent(jLabelTipo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                                .addComponent(jText1, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE))
                            .addGap(107, 107, 107)))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTipo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(jLabelTipoPagamento))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jText1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jLabelTotal)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jText2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(EntradaOUValorCartOUNCupom)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jText3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelValorPagoOuParcelaOuValorCupom)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelCartaoOuTroco)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(70, 70, 70))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
                                         
    private void jTextField8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField8ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField8ActionPerformed

    private void jText3KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jText3KeyPressed
       /*ESCOLHER TIPO DE PAGAMENTO*/
        
         /*DINHEIRO*/
         if(evt.getKeyCode() == KeyEvent.VK_F1){
            System.out.print("F2");
            jText1.setText("DINHEIRO");            
            tipoPagamento = TipoPagamento.Dinheiro;
            EntradaOUValorCartOUNCupom.setVisible(false);
            
            
            
        }
         if(evt.getKeyCode() == KeyEvent.VK_ENTER){
              System.out.print("F8");
              registrarPagamento();
             
              
          }
        /*CARTAO*/
        if(evt.getKeyCode() == KeyEvent.VK_F2){
            jText1.setText("CARTÃO");  
            this.tipoPagamento = TipoPagamento.Cartao;
         }
         /*troca*/
        if(evt.getKeyCode() == KeyEvent.VK_F3){
            jText1.setText("CUPOM TROCA");  
            this.tipoPagamento = TipoPagamento.Troca;
         }
         /*REGISTRA O PAGAMENTO*/
          
}//GEN-LAST:event_jText3KeyPressed

    private void jTextField4KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField4KeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
           //  Double valorPgto = Double.parseDouble(jTextField4.getText());
            //  jTextField5.setText(""+(valorPgto-venda.getValor()));
             
              
          }
    }//GEN-LAST:event_jTextField4KeyPressed
     private void registrarPagamento() {
        switch(this.tipoPagamento){ 
                  case Dinheiro: adicionarPagamentoDinheiro();
                  break;
                  case Cartao: adicionarPagamentoCartao();
                  break;
                  case Troca: adicionarPagamanetoTroca();
                  break;
              }
    }

    /**
     * @param args the command line arguments
     */
  
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel EntradaOUValorCartOUNCupom;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabelCartaoOuTroco;
    private javax.swing.JLabel jLabelTipo;
    private javax.swing.JLabel jLabelTipoPagamento;
    private javax.swing.JLabel jLabelTotal;
    private javax.swing.JLabel jLabelValorPagoOuParcelaOuValorCupom;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jText1;
    private javax.swing.JTextField jText2;
    private javax.swing.JTextField jText3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    // End of variables declaration//GEN-END:variables

   
}

